package io.renren.modules.auto.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.URLUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.auto.entity.AtDroolsEntity;
import io.renren.modules.auto.service.AtDroolsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 服务规则管理表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-11-29 14:11:45
 */
@RestController
@RequestMapping("auto/atdrools")
public class AtDroolsController {
    @Autowired
    private AtDroolsService atDroolsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("auto:atdrools:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = atDroolsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("auto:atdrools:info")
    public R info(@PathVariable("id") Long id){
		AtDroolsEntity atDrools = atDroolsService.getById(id);

        return R.ok().put("atDrools", atDrools);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("auto:atdrools:save")
    public R save(@RequestBody AtDroolsEntity atDrools){

        //转换antlr语法

		atDroolsService.save(atDrools);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("auto:atdrools:update")
    public R update(@RequestBody AtDroolsEntity atDrools){
		atDroolsService.updateById(atDrools);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("auto:atdrools:delete")
    public R delete(@RequestBody Long[] ids){
		atDroolsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 注册
     */
    @RequestMapping("/register/{id}")
    public R register(@PathVariable("id") Long id){
        AtDroolsEntity atDroolsEntity = atDroolsService.getById(id);
        if (atDroolsEntity.getRegisterStatus().equals("1")) {
            return R.error("已注册，不可重复注册！");
        }
        URLUtils.get(atDroolsEntity.getRegisterUrl());
        atDroolsEntity.setRegisterStatus("1");
        atDroolsService.updateById(atDroolsEntity);
        return R.ok();
    }

}
